/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.pms.controller;

import com.fzhucc.annotation.Log;
import com.fzhucc.modules.pms.domain.ProjectService;
import com.fzhucc.modules.pms.domain.dto.ProjectServiceWithProjectIdDto;
import com.fzhucc.modules.pms.service.ProjectServiceService;
import com.fzhucc.modules.pms.domain.dto.ProjectServiceQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author yyg
 * @date 2022-12-13
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "项目库服务管理")
@RequestMapping("/api/projectService")
public class ProjectServiceController {

    private final ProjectServiceService projectServiceService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectService:list')")
    public void exportProjectService(HttpServletResponse response, ProjectServiceQueryCriteria criteria) throws IOException {
        projectServiceService.download(projectServiceService.queryAll(criteria), response);
    }


    @Log("查询projectService")
    @ApiOperation("查询projectService")
    @PreAuthorize("@el.check('projectService:list')")
    public ResponseEntity<Object> queryProjectService(ProjectServiceQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectServiceService.queryAll(criteria,pageable), HttpStatus.OK);
    }

    @GetMapping("/queryProjectByProjectId")
    @Log("根据项目id查询项目服务情况")
    @ApiOperation("根据项目id查询项目服务情况")
    @PreAuthorize("@el.check('projectService:list')")
    public ResponseEntity<Object> queryProjectByProjectId(ProjectServiceWithProjectIdDto projectServiceWithProjectIdDto, Pageable pageable){
        return new ResponseEntity<>(projectServiceService.queryProjectByProjectId(projectServiceWithProjectIdDto,pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增projectService")
    @ApiOperation("新增projectService")
    @PreAuthorize("@el.check('projectService:add')")
    public ResponseEntity<Object> createProjectService(@Validated @RequestBody ProjectService resources){
        return new ResponseEntity<>(projectServiceService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改projectService")
    @ApiOperation("修改projectService")
    @PreAuthorize("@el.check('projectService:edit')")
    public ResponseEntity<Object> updateProjectService(@Validated @RequestBody ProjectService resources){
        projectServiceService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除projectService")
    @ApiOperation("删除projectService")
    @PreAuthorize("@el.check('projectService:del')")
    public ResponseEntity<Object> deleteProjectService(@RequestBody Integer[] ids) {
        projectServiceService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}